package com.linguochao.rabbitmq.comsumer;

import com.linguochao.rabbitmq.config.RabbitDelayConfig;
import com.linguochao.rabbitmq.config.RabbitMQConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @author linguochao
 * @Description 死信队列
 * @Date 2020/7/28 18:13
 */
@Slf4j
@Component
public class DeadLetterQueueConsumer {

    /**
     * 手动ACK方式
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues = RabbitDelayConfig.DEAD_LETTER_QUEUE_NAME)
    public void receiveDo1(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        log.info("当前时间：{},死信队列Do1收到消息：{}", new Date().toString(), msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    @RabbitListener(queues = RabbitDelayConfig.DEAD_LETTER_QUEUE_NAME)
    public void sendMessage(String msg) {
        log.info("当前时间：{},死信队列Do1收到消息：{}", new Date().toString(), msg);
        System.out.println("发送短信成功,用户:" + msg);
    }
}